/******************************************************************************
 * @File name   :      CommonQueryServiceImpl.java
 *
 * @Author      :      guyifeng
 *
 * @Date        :      2011-6-27
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 DMS, Inc. All  Rights Reserved.
 * This software is published under the terms of the DMS Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2011-6-27 上午11:00:37        guyifeng     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.framework.common.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cap.bts.commonfunc.common.util.UserUtil;
import com.cap.bts.commonfunc.ge.dealer.service.IDealerService;
import com.cap.bts.framework.codemapping.service.ICodeMapping;
import com.cap.bts.framework.common.dto.ApprovalUserGrpAppResultDto;
import com.cap.bts.framework.common.dto.EmployeeDetailDto;
import com.cap.bts.framework.common.exception.AppException;
import com.cap.bts.framework.common.service.ICommonQueryService;
import com.cap.bts.framework.common.util.Pager;
import com.cap.bts.framework.dm.employeemgnt.dao.ITempOrganizationUserDao;
import com.cap.bts.framework.dm.employeemgnt.dao.ITempUserUsrGrpDao;
import com.cap.bts.framework.dm.employeemgnt.domain.TempOrganizationUser;
import com.cap.bts.framework.dm.employeemgnt.domain.TempUserUsrGrp;
import com.cap.bts.framework.dm.employeemgnt.dto.UserGroupOrgDto;
import com.cap.bts.framework.usermgnt.dao.IOrganizationDao;
import com.cap.bts.framework.usermgnt.dao.IRoleDao;
import com.cap.bts.framework.usermgnt.dao.IUserDao;
import com.cap.bts.framework.usermgnt.dao.IUserGroupDao;
import com.cap.bts.framework.usermgnt.domain.Organization;
import com.cap.bts.framework.usermgnt.domain.Role;
import com.cap.bts.framework.usermgnt.domain.User;
import com.cap.bts.framework.usermgnt.domain.UserGroup;
import com.cap.bts.framework.usermgnt.service.IEmployeeInfoService;
import com.cap.bts.framework.usermgnt.service.IOrganizationMgntService;

/**
 * Common Query Service Implementation Class
 */
@Transactional
@Service
public class CommonQueryServiceImpl implements ICommonQueryService {

	
	@Resource(name = "userGroupDao")
    private IUserGroupDao userGroupDao;
    
    @Resource(name = "roleDao")
    private IRoleDao roleDao;
    
    @Resource(name = "organizationDao")
    private IOrganizationDao organizationDao;
    
    @Resource(name = "tempOrganizationUserDao")
    private ITempOrganizationUserDao tempOrganizationUserDao;
    @Resource(name = "tempUserUsrGrpDao")
    private ITempUserUsrGrpDao tempUserUsrGrpDao;
    @Autowired
    private IOrganizationMgntService orgMgntService;
    
    @Resource(name = "userDao")
    private   IUserDao userDao;
  
    @Autowired
    private IEmployeeInfoService employeeService;
   
    @Autowired
    private IDealerService dealerService;
    
    @Autowired
    ICodeMapping codeMappingService;

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2011-6-21
     * @see com.cap.bts.gdms.common.service.ICommonQueryService#getRoleWithPager(com.cap.bts.framework.common.dto.BasePagerDto)
    **/
    @Override
    public List<Role> getRoleWithPager(int roleType,Pager pager) throws AppException {
//      List<Role> roles = roleDao.getRolesWithPaging(roleType, pager);
        User _user = UserUtil.getCurrentUser();
        List<Role> roles = roleDao.getRolesWithPaging(_user.getBrandId(), roleType, pager);
        return roles;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2011-6-21
     * @see com.cap.bts.gdms.common.service.ICommonQueryService#getOrganizationWithPager(com.cap.bts.framework.common.dto.BasePagerDto)
    **/
    @Override
    public List<Organization> getOrganizationWithPager(Integer[] orgTypes, Pager pager) throws AppException {
        List<Organization> organizations = organizationDao.getOrganizationsByTypesWithPaging(orgTypes, pager);
        return organizations;
    }


    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2011-6-30
    **/
    @Override
    public EmployeeDetailDto getEmployeeDetailOrgAndUserGroupAndRole(
        EmployeeDetailDto employeedetaildto, User user) throws AppException {
    	return null;
    }
	
    /**
     * 根据userCode得到List<UserGroupOrgDto>
     * @Date        :      2011-6-30
     * @param userCode String 
     * @return List<UserGroupOrgDto> 
     * @throws AppException 
     */
    private List<UserGroupOrgDto> getUserGroupOrgDto(String userCode) throws AppException {

        List<Organization> orgList = organizationDao.getOrganizationsByUserCode(userCode);
        List<UserGroup> userGroupList = userGroupDao.getUserGroupByUserCode(userCode);  
        
         
        if (listIsNotEmpty(orgList) && listIsNotEmpty(userGroupList)){
            
            Organization org = orgList.get(0);
            UserGroup userGroup = userGroupList.get(0);
            
            String parentOrgCode = org.getParentOrgCode();
        	Organization parentOrg = orgMgntService.getOrganizationByOrgCode(parentOrgCode);

            List<UserGroupOrgDto> userGroupOrgDtoList = new ArrayList<UserGroupOrgDto>();

            List<User> users = orgMgntService.getManagerUserByOrgCodeAndUsrGrpCode(
                userGroup.getUserGroupCode(), org.getOrgCode());

            StringBuilder userStr = new StringBuilder();
            if (users != null && users.size() > 0) {
                for (int i = 0; i < users.size(); i++) {
                    User userTemp = users.get(i);
                    userStr.append(userTemp.getName() + ",");
                }
                if (userStr.length() > 0) {
                    userStr.deleteCharAt(userStr.length() - 1);
                }
            }

            UserGroupOrgDto userGroupOrgDto = new UserGroupOrgDto();
            userGroupOrgDto.setUserGroupCode(userGroup.getUserGroupCode());
            userGroupOrgDto.setUserGroupName(userGroup.getUserGroupName());
            userGroupOrgDto.setDefUserGroupName(userGroup.getUserGroupName());
            userGroupOrgDto.setOrgName(org.getOrgName());
            userGroupOrgDto.setOrgManager(userStr.toString());
            userGroupOrgDto.setAdvancedOrgName(parentOrg!=null?parentOrg.getOrgName():"N/A");
            userGroupOrgDto.setOrgCode(org.getOrgCode());
            userGroupOrgDtoList.add(userGroupOrgDto); 
            
            return userGroupOrgDtoList; 
        }
        
        
       return null;
       
    }
   
    private boolean listIsNotEmpty(List list)
    {
        if ((list != null) && (list.size() > 0)){
            return true;
        }else{
            return false;
        }
        
    }
    
    /**
     * 根据条件得到 UserGroupOrgDto
     * 
     * @Date : 2011-6-29
     * @param userCode
     *            String
     * @param defaultUserGroupCode
     *            String
     * @param ownerId
     *            String
     * @return List<UserGroupOrgDto>
     * @throws AppException
     */
    private List<UserGroupOrgDto> getUserGroupOrgDto(TempOrganizationUser tempOrgUser,
        TempUserUsrGrp tempUserUsrGrp) throws AppException {

        Organization org = organizationDao.findById(tempOrgUser.getOrganizationId());

        UserGroup userGroup = userGroupDao.findById(tempUserUsrGrp.getUserGroupId());
 
        if (org != null && userGroup != null) {
            
            String parentOrgCode = org.getParentOrgCode();
            Organization parentOrg = orgMgntService.getOrganizationByOrgCode(parentOrgCode);

            List<UserGroupOrgDto> userGroupOrgDtoList = new ArrayList<UserGroupOrgDto>();

            List<User> users = orgMgntService.getManagerUserByOrgCodeAndUsrGrpCode(
                userGroup.getUserGroupCode(), org.getOrgCode());

            StringBuilder userStr = new StringBuilder();
            if (users != null && users.size() > 0) {
                for (int i = 0; i < users.size(); i++) {
                    User userTemp = users.get(i);
                    userStr.append(userTemp.getName() + ",");
                }
                if (userStr.length() > 0) {
                    userStr.deleteCharAt(userStr.length() - 1);
                }
            }

            UserGroupOrgDto userGroupOrgDto = new UserGroupOrgDto();
            userGroupOrgDto.setUserGroupCode(userGroup.getUserGroupCode());
            userGroupOrgDto.setUserGroupName(userGroup.getUserGroupName());
            userGroupOrgDto.setDefUserGroupName(userGroup.getUserGroupName());
            userGroupOrgDto.setOrgName(org.getOrgName());
            userGroupOrgDto.setOrgManager(userStr.toString());
            if (parentOrg != null){
                userGroupOrgDto.setAdvancedOrgName(parentOrg.getOrgName());
            }else {
                userGroupOrgDto.setAdvancedOrgName("N/A");
            }
            userGroupOrgDto.setOrgCode(org.getOrgCode());
            userGroupOrgDtoList.add(userGroupOrgDto);

            return userGroupOrgDtoList;
            
        }
            
          return null;
       
    }

   
 
    
    public ApprovalUserGrpAppResultDto getApproveRuleListByEmployeeCode(String employeeCode)throws AppException{
    	return null;
    }

}
